import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Op } from "sequelize";
import { getPageWhere, getPageData } from '@/utils/page';
import { RESPONSE_CODE } from '@/enums/code';
import { PlatformInfo } from './platformInfo.model';
import { GetPlatformInfoPageDto } from './platformInfo.dto'


@Injectable()
export class PlatformInfoService {
  constructor(
    @InjectModel(PlatformInfo)
    private platformInfoModel: typeof PlatformInfo,

  ) { }

  /**
   * 资源平台分页
   * @method post
   * @param getPlatformInfoPageDto 
   * @returns
   */
  async getPlatformInfoPage(getPlatformInfoPageDto: GetPlatformInfoPageDto) {
    const {
      current,
      size,
      factoryId,
      orgId,
      appId,
      renterId,
      displayName,
      platformCode,
      indexSource,
      tokenType,
    } = getPlatformInfoPageDto;
    const query = {};
    if (displayName) {
      query["displayName"] = { [Op.like]: `${displayName}%` };
    }
    if (platformCode) {
      query["platformCode"] = { [Op.like]: `${platformCode}%` };
    }
    if (indexSource) {
      query["indexSource"] = indexSource;
    }
    if (tokenType) {
      query["tokenType"] = tokenType;
    }
    if (factoryId) {
      query["factoryId"] = factoryId;
    }

    const { count, rows } = await this.platformInfoModel.findAndCountAll({
      where: query,
      ...getPageWhere(current, size),
    });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: getPageData(current, size, count, rows),
      msg: null,
    }
  }
}
